package com.woniuxy.purchasemanager.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.commons.entity.purchaseManager.*;
import com.woniuxy.commons.entity.purchaseManager.base.VoEntity;
import com.woniuxy.purchasemanager.dao.*;
import com.woniuxy.purchasemanager.service.NeedAllocationService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("needAllocationService")
public class NeedAllocationServiceImpl extends ServiceImpl<NeedAllocationDao, NeedAllocationEntity> implements NeedAllocationService {

    @Resource
    private NeedAllocationDao allocationDao;

    @Resource
    private NeedAndGoodsDao needAndGoodsDao;

    @Resource
    private NeedAllocationAndNeedDemandGoodsDao needAllocationAndNeedDemandGoodsDao;

    @Resource
    private NeedDemandGoodsDao needDemandGoods;


    @Resource
    private PurchaseOrderDao purchaseOrderDao;
    @Resource
    PurchaseOrderGoodsDao purchaseOrderGoodsDao;
    @Override
    public VoEntity<NeedAllocationEntity> pageListAll(Integer currentPage, Integer pageSize) {
        VoEntity voEntity = new VoEntity<>();
        IPage<NeedAllocationEntity> page = new Page<>(currentPage, pageSize);//参数一是当前页，参数二是每页个数

        baseMapper.selectPage(page, null);
        voEntity.setSize(pageSize);
        voEntity.setTotal(page.getTotal());
        voEntity.setCurrent(currentPage);
        voEntity.setList(page.getRecords());
        return voEntity;
    }

    @Override
    public  Map<String, Object> getOrderGoodsList(String needOrderId) {
        List array = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<NeedAllocationEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("odd_number", needOrderId);
        NeedAllocationEntity needAllocationEntity = allocationDao.selectOne(queryWrapper);
        map.put("g",needAllocationEntity);
        Integer allocationDemandId = needAllocationEntity.getAllocationDemandId();
        QueryWrapper<needAllocationAndNeedDemandGoods> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("allocation_demand_id", allocationDemandId);
        List<needAllocationAndNeedDemandGoods> needAllocationDemand = needAllocationAndNeedDemandGoodsDao.selectList(queryWrapper1);
        for (needAllocationAndNeedDemandGoods a : needAllocationDemand) {
            array.add(a.getNeedDemanedProductId());
        }

        List array1 = new ArrayList<>();
        for (int i = 0; i < array.size(); i++)
        {
            QueryWrapper<NeedDemandGoodsEntity> query = new QueryWrapper();
            query.eq("allocation_demand_id",array.get(i));
            NeedDemandGoodsEntity needDemandGoodsEntity = needDemandGoods.selectOne(query);
           array1.add(needDemandGoodsEntity);
        }
        map.put("goodsInfo",array1);
        return map;
    }

    @Override
    public boolean addList(Map<String, Object> map) {
        HashMap<String, Object> basicInfo = (HashMap<String, Object>) map.get("basicInfo");
        ArrayList<HashMap<String, Object>> goodsInfo = (ArrayList<HashMap<String, Object>>) map.get("goodsInfo");


        ArrayList arrayList = new ArrayList<>();
       for(HashMap<String, Object> goo : goodsInfo){
           NeedDemandGoodsEntity needDemandGoodsEntity = JSON.parseObject(JSON.toJSONString(goo), NeedDemandGoodsEntity.class);
            PurchaseOrderGoodsEntity purchaseOrderGoodsEntity = new PurchaseOrderGoodsEntity();
           purchaseOrderGoodsEntity.setProductId(needDemandGoodsEntity.getNeedDemandProductId());
           purchaseOrderGoodsEntity.setWeightStart(needDemandGoodsEntity.getGramStart());
           purchaseOrderGoodsEntity.setWeightEnd(needDemandGoodsEntity.getGramEnd());
           purchaseOrderGoodsDao.insert(purchaseOrderGoodsEntity);
           Integer productId = purchaseOrderGoodsEntity.getPurchaseOrderGoodsId();
           arrayList.add(productId);
       }

                NeedEntity needEntity = JSON.parseObject(JSON.toJSONString(basicInfo), NeedEntity.class);
        PurchaseOrderEntity purchaseOrderEntity = new PurchaseOrderEntity();
        purchaseOrderEntity.setOrderNo(needEntity.getOddNumber());
        purchaseOrderEntity.setCustomer(needEntity.getClient());
        purchaseOrderEntity.setPurWeight(needEntity.getWeight());
        purchaseOrderEntity.setPurNums(needEntity.getGoodsAmount());
        purchaseOrderEntity.setProcurement(needEntity.getSingle());
        purchaseOrderEntity.setPurchaseOrderStatus(needEntity.getStatus());
        purchaseOrderEntity.setProductLine(needEntity.getProductLine());
        purchaseOrderEntity.setWarehouse(needEntity.getWarehouse());
        purchaseOrderEntity.setAudit(needEntity.getAudit());
        purchaseOrderEntity.setMakPeople(needEntity.getSingle());
        purchaseOrderEntity.setCondition(needEntity.getCondition());

        purchaseOrderDao.insert(purchaseOrderEntity);
        Integer purchaseOrderId = purchaseOrderEntity.getPurchaseOrderId();

        purchaseOrderDao.insertNewMiddleTable(purchaseOrderId,arrayList);

        return true;
    }
}